package view;

import java.util.LinkedList;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;

import control.OrderControl;

import adamsLibrary.JComboBoxAuto;
import adamsLibrary.JComboBoxAutoLinkedList;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JScrollPane;

public class BlockedProductPanel extends JPanel {
	private OrderControl oc;
	private JComboBoxAuto comboBox_1;
	private DefaultListModel listModel;
	private JButton btnListBlockedProducts;
	private JLabel lblListBlockedProducts;
	private JButton btnList;
	private JScrollPane scrollPane;
	private JList textArea;
	public BlockedProductPanel(OrderControl oc) {
		setBackground(Color.BLACK);
		this.oc = oc;
		
		JLabel lblProduct = new JLabel("List blocked barcodes");
		lblProduct.setForeground(Color.GREEN);
		comboBox_1 = new JComboBoxAuto(new getProducts());
		
		listModel = new DefaultListModel();
		textArea = new JList(listModel);
		textArea.setBackground(Color.BLACK);
		textArea.setForeground(Color.GREEN);
		
		btnListBlockedProducts = new JButton("List all");
		btnListBlockedProducts.addActionListener(new GetBlockedProducts());
		
		lblListBlockedProducts = new JLabel("List blocked products");
		lblListBlockedProducts.setForeground(Color.GREEN);
		
		btnList = new JButton("List");
		btnList.addActionListener(new GetBlockedPallets());
		
		scrollPane = new JScrollPane(textArea);
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 538, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblProduct)
								.addComponent(lblListBlockedProducts))
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addGroup(groupLayout.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, 146, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(btnList, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(158)
									.addComponent(btnListBlockedProducts)))))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblProduct)
						.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnList))
					.addGap(5)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblListBlockedProducts)
						.addComponent(btnListBlockedProducts))
					.addGap(18)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
					.addContainerGap())
		);
		setLayout(groupLayout);
	}
	
	private class getProducts implements JComboBoxAutoLinkedList{
		@Override
		public LinkedList<String> getList(String string) {
			return oc.getProducts(string);
		}		
	}
	
	private class GetBlockedPallets implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			String product = comboBox_1.getEditor().getItem().toString();
			
			LinkedList<String> infoList = oc.getBlockedPalletsByProduct(product);
			listModel.clear();
			for(String info:infoList){
				listModel.addElement(info);
			}
			
		}		
	}
	
	private class GetBlockedProducts implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {			
			LinkedList<String> infoList = oc.getBlockedProducts();
			listModel.clear();
			for(String info:infoList){
				listModel.addElement(info);
			}
			
		}		
	}
}
